﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.Services.Mail;
    using DotNetNuke.UI.Skins;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminReport : BaseAdminModule
    {
        [AccessedThroughProperty("AdminReportEdit")]
        private NEvoWeb.Modules.NB_Store.AdminReportEdit _AdminReportEdit;
        [AccessedThroughProperty("AdminReportList")]
        private NEvoWeb.Modules.NB_Store.AdminReportList _AdminReportList;
        [AccessedThroughProperty("cmdFormReturn")]
        private LinkButton _cmdFormReturn;
        [AccessedThroughProperty("cmdFormRun")]
        private LinkButton _cmdFormRun;
        [AccessedThroughProperty("cmdResultsReturn")]
        private LinkButton _cmdResultsReturn;
        [AccessedThroughProperty("dgResults")]
        private DataGrid _dgResults;
        private string _frm;
        [AccessedThroughProperty("frmReport")]
        private CustomForm _frmReport;
        [AccessedThroughProperty("lblMessage")]
        private Label _lblMessage;
        [AccessedThroughProperty("pnlEdit")]
        private Panel _pnlEdit;
        [AccessedThroughProperty("pnlForm")]
        private Panel _pnlForm;
        [AccessedThroughProperty("pnlList")]
        private Panel _pnlList;
        [AccessedThroughProperty("pnlResults")]
        private Panel _pnlResults;
        private int RepID;

        public AdminReport()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            this.RepID = -1;
            this._frm = "";
        }

        private void AdminReportEdit_AddParamButton(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("RepID", this.RepID.ToString(), "AdminReport"));
        }

        private void AdminReportEdit_CancelButton(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("AdminReport"));
        }

        private void AdminReportEdit_DeleteButton(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("AdminReport"));
        }

        private void AdminReportEdit_SearchButton(object sender, EventArgs e)
        {
            this.populateList();
        }

        private void AdminReportEdit_UpdateButton(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("AdminReport"));
        }

        private void AdminReportList_AddButton(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("RepID", "0", "AdminReport"));
        }

        private void AdminReportList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            int num = int.Parse(e.CommandArgument.ToString());
            string commandName = e.CommandName;
            if (commandName == "View")
            {
                this.RedirectToForm(Conversions.ToString(num), "view");
                this.ViewReport(Conversions.ToString(num));
            }
            else if (commandName == "Export")
            {
                this.RedirectToForm(Conversions.ToString(num), "export");
                this.ExportToExcel(num);
            }
            else if (commandName == "EditRep")
            {
                this.Response.Redirect(this.EditUrl("RepID", num.ToString(), "AdminReport"));
            }
            else if (commandName == "Email")
            {
                this.RedirectToForm(Conversions.ToString(num), "email");
                this.EmailReport(Conversions.ToString(num));
            }
            else
            {
                this.Response.Redirect(this.EditUrl("AdminReport"));
            }
        }

        private void cmdFormReturn_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("AdminReport"));
        }

        private void cmdFormRun_Click(object sender, EventArgs e)
        {
            string xMLData = this.frmReport.XMLData;
            if (this._frm.ToLower() == "view")
            {
                this.ViewReport(Conversions.ToString(this.RepID));
            }
            else if (this._frm.ToLower() == "export")
            {
                this.ExportToExcel(this.RepID);
            }
            else if (this._frm.ToLower() == "email")
            {
                this.EmailReport(Conversions.ToString(this.RepID));
            }
        }

        private void cmdResultsReturn_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(this.EditUrl("AdminReport"));
        }

        private void EmailReport(string ReportID)
        {
            try
            {
                SQLReportController controller = new SQLReportController();
                this.lblMessage.Text = DotNetNuke.Services.Localization.Localization.GetString("QueryError", this.LocalResourceFile);
                this.pnlResults.Visible = true;
                this.pnlList.Visible = false;
                this.lblMessage.Visible = true;
                NB_Store_SQLReportInfo sQLReport = controller.GetSQLReport(Conversions.ToInteger(ReportID));
                if ((sQLReport != null) && sQLReport.EmailResults)
                {
                    sQLReport.SQL = controller.insertParams(Conversions.ToInteger(ReportID), this.UserId, this.PortalId, this.Request, sQLReport.SQL, SharedFunctions.GetCurrentCulture());
                    sQLReport.SQL = controller.insertFormParams(Conversions.ToInteger(ReportID), sQLReport.SQL, this.frmReport.XMLData);
                    string str2 = "";
                    if (sQLReport.ShowSQL)
                    {
                        this.lblMessage.Text = sQLReport.SQL + "<br /><hr />" + DotNetNuke.Services.Localization.Localization.GetString("EmailMsg", this.LocalResourceFile) + sQLReport.EmailTo + "<hr />";
                    }
                    else
                    {
                        this.lblMessage.Text = DotNetNuke.Services.Localization.Localization.GetString("EmailMsg", this.LocalResourceFile) + sQLReport.EmailTo + "<hr />";
                    }
                    string body = "";
                    str2 = controller.runXSL(sQLReport);
                    if (str2 != "")
                    {
                        if (str2 != "HIDE_XSL_OUTPUT")
                        {
                            body = str2;
                        }
                        else
                        {
                            body = DotNetNuke.Services.Localization.Localization.GetString("nooutput", this.LocalResourceFile);
                        }
                    }
                    else
                    {
                        controller.popDataGridSQL(sQLReport.SQL, this.dgResults);
                        StringBuilder sb = new StringBuilder();
                        StringWriter writer2 = new StringWriter(sb);
                        HtmlTextWriter writer = new HtmlTextWriter(writer2);
                        this.dgResults.RenderControl(writer);
                        body = sb.ToString();
                        string path = this.Server.MapPath(base.StoreInstallPath + "module.css");
                        if (File.Exists(path))
                        {
                            FileObj obj2 = new FileObj();
                            string errInfo = "";
                            body = "<style type=\"text/css\">" + obj2.GetFileContents(path, ref errInfo) + "</style>" + body;
                        }
                    }
                    if (body != "")
                    {
                        body = sQLReport.ReportTitle + "<br/>" + body;
                        DotNetNuke.Services.Mail.Mail.SendMail(sQLReport.EmailFrom, sQLReport.EmailTo, "", sQLReport.ReportName, body, "", "HTML", "", "", "", "");
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.lblMessage.Text = exception1.ToString() + "<hr />";
                ProjectData.ClearProjectError();
            }
        }

        public void ExportToExcel(int ReportID)
        {
            SQLReportController controller = new SQLReportController();
            try
            {
                NB_Store_SQLReportInfo sQLReport = controller.GetSQLReport(ReportID);
                if (sQLReport != null)
                {
                    this.Response.Clear();
                    sQLReport.SQL = controller.insertParams(ReportID, this.UserId, this.PortalId, this.Request, sQLReport.SQL, SharedFunctions.GetCurrentCulture());
                    sQLReport.SQL = controller.insertFormParams(ReportID, sQLReport.SQL, this.frmReport.XMLData);
                    string s = "";
                    s = controller.runXSL(sQLReport);
                    if (s != "")
                    {
                        this.Response.AddHeader("content-disposition", "attachment; filename=" + Strings.Replace(sQLReport.ReportName, " ", "_", 1, -1, CompareMethod.Binary));
                        this.Response.Charset = "";
                        this.Response.ContentType = "application/octet-stream";
                        if (s != "HIDE_XSL_OUTPUT")
                        {
                            this.Response.Write(s);
                        }
                        else
                        {
                            this.Response.Write(DotNetNuke.Services.Localization.Localization.GetString("nooutput", this.LocalResourceFile));
                        }
                        this.Response.Flush();
                    }
                    else
                    {
                        if (sQLReport.FieldDelimeter == "t")
                        {
                            sQLReport.FieldDelimeter = "\t";
                        }
                        this.Response.AddHeader("content-disposition", "attachment; filename=" + Strings.Replace(sQLReport.ReportName, " ", "_", 1, -1, CompareMethod.Binary) + ".csv");
                        this.Response.Charset = "";
                        this.Response.ContentType = "application/vnd.xls";
                        IDataReader reader = controller.ExecuteSQL(sQLReport.SQL);
                        StringBuilder builder = new StringBuilder();
                        int num3 = reader.FieldCount - 1;
                        for (int i = 0; i <= num3; i++)
                        {
                            if (reader.GetName(i) != null)
                            {
                                builder.Append(reader.GetName(i));
                            }
                            if (i < (reader.FieldCount - 1))
                            {
                                builder.Append(sQLReport.FieldDelimeter);
                            }
                        }
                        this.Response.Write(builder.ToString() + "\r\n");
                        this.Response.Flush();
                        while (reader.Read())
                        {
                            builder = new StringBuilder();
                            int num4 = reader.FieldCount - 2;
                            for (int j = 0; j <= num4; j++)
                            {
                                if (Strings.Replace(sQLReport.FieldQualifier, " ", "", 1, -1, CompareMethod.Binary) == "")
                                {
                                    if (!reader.IsDBNull(j))
                                    {
                                        builder.Append(reader.GetValue(j).ToString().Replace(sQLReport.FieldDelimeter, " "));
                                    }
                                }
                                else
                                {
                                    builder.Append(sQLReport.FieldQualifier);
                                    builder.Append(reader.GetValue(j).ToString());
                                    builder.Append(sQLReport.FieldQualifier);
                                }
                                builder.Append(sQLReport.FieldDelimeter);
                            }
                            if (Strings.Replace(sQLReport.FieldQualifier, " ", "", 1, -1, CompareMethod.Binary) == "")
                            {
                                if (!reader.IsDBNull(reader.FieldCount - 1))
                                {
                                    builder.Append(reader.GetValue(reader.FieldCount - 1).ToString().Replace(sQLReport.FieldDelimeter, " "));
                                }
                            }
                            else if (!reader.IsDBNull(reader.FieldCount - 1))
                            {
                                builder.Append(sQLReport.FieldQualifier);
                                builder.Append(reader.GetValue(reader.FieldCount - 1).ToString());
                                builder.Append(sQLReport.FieldQualifier);
                            }
                            this.Response.Write(builder.ToString() + "\n");
                            this.Response.Flush();
                        }
                        reader.Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                this.Response.Write(exception.Message);
                ProjectData.ClearProjectError();
            }
            this.Response.End();
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.RepID = -1;
            if ((this.Request.QueryString["RepID"] != null) && Versioned.IsNumeric(this.Request.QueryString["RepID"]))
            {
                this.RepID = Conversions.ToInteger(this.Request.QueryString["RepID"]);
            }
            this._frm = "";
            if (this.Request.QueryString["frm"] != null)
            {
                this._frm = this.Request.QueryString["frm"];
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.AdminReportEdit.RepID = this.RepID;
                this.AdminReportList.IsEdit = this.IsEditable;
                if (!this.Page.IsPostBack)
                {
                    if (this.Request.QueryString["ReportRef"] != null)
                    {
                        this.RunByRef(this.Request.QueryString["ReportRef"]);
                    }
                    else if ((this.RepID >= 0) & (this._frm == ""))
                    {
                        this.populateEdit(Conversions.ToString(this.RepID));
                    }
                    else
                    {
                        this.populateList();
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void populateEdit(string RepID)
        {
            this.pnlList.Visible = false;
            this.pnlResults.Visible = false;
            this.pnlForm.Visible = false;
            this.pnlEdit.Visible = true;
            this.AdminReportEdit.populateEdit(Conversions.ToInteger(RepID));
        }

        private void populateForm(string RepID)
        {
            this.pnlList.Visible = false;
            this.pnlResults.Visible = false;
            this.pnlForm.Visible = true;
            this.pnlEdit.Visible = false;
        }

        private void populateList()
        {
            if (this._frm != "")
            {
                this.pnlList.Visible = false;
                this.pnlForm.Visible = true;
            }
            else
            {
                this.pnlList.Visible = true;
                this.pnlForm.Visible = false;
            }
            this.pnlResults.Visible = false;
            this.pnlEdit.Visible = false;
            if (this.UserInfo.IsSuperUser | this.UserInfo.IsInRole("Administrator"))
            {
                this.AdminReportList.IsEdit = true;
            }
            else
            {
                this.AdminReportList.IsEdit = false;
            }
            this.AdminReportList.populateList();
        }

        private void RedirectToForm(string ReportID, string ExeType)
        {
            IEnumerator enumerator=null;
            SQLReportController controller = new SQLReportController();
            bool flag = false;
            ArrayList sQLReportParamList = controller.GetSQLReportParamList(Conversions.ToInteger(ReportID));
            try
            {
                enumerator = sQLReportParamList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SQLReportParamInfo current = (NB_Store_SQLReportParamInfo) enumerator.Current;
                    if (current.ParamSource > 7)
                    {
                        flag = true;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            if (flag)
            {
                this.Response.Redirect(this.EditUrl("RepID", ReportID, "AdminReport", new string[] { "frm", ExeType }));
            }
        }

        private void RunByRef(string ReportRef)
        {
            NB_Store_SQLReportInfo sQLReportByRef = new SQLReportController().GetSQLReportByRef(this.PortalId, ReportRef);
            if (sQLReportByRef == null)
            {
                this.lblMessage.Text = DotNetNuke.Services.Localization.Localization.GetString("InvalidReportRef", this.LocalResourceFile);
            }
            else
            {
                this.ViewReport(Conversions.ToString(sQLReportByRef.ReportID));
            }
        }

        private void ViewReport(string ReportID)
        {
            string str = "ERROR IN REPORT";
            try
            {
                SQLReportController controller = new SQLReportController();
                this.lblMessage.Text = DotNetNuke.Services.Localization.Localization.GetString("QueryError", this.LocalResourceFile);
                this.pnlResults.Visible = true;
                this.pnlList.Visible = false;
                this.lblMessage.Visible = true;
                NB_Store_SQLReportInfo sQLReport = controller.GetSQLReport(Conversions.ToInteger(ReportID));
                if (sQLReport != null)
                {
                    if (sQLReport.DisplayInLine)
                    {
                        sQLReport.SQL = controller.insertParams(Conversions.ToInteger(ReportID), this.UserId, this.PortalId, this.Request, sQLReport.SQL, SharedFunctions.GetCurrentCulture());
                        sQLReport.SQL = controller.insertFormParams(Conversions.ToInteger(ReportID), sQLReport.SQL, this.frmReport.XMLData);
                        string str2 = "";
                        str2 = controller.runXSL(sQLReport);
                        if (str2 == "")
                        {
                            if (sQLReport.SQL != "")
                            {
                                if (sQLReport.ShowSQL)
                                {
                                    this.lblMessage.Text = sQLReport.SQL + "<br /><hr />";
                                }
                                else
                                {
                                    this.lblMessage.Text = "";
                                }
                                controller.popDataGridSQL(sQLReport.SQL, this.dgResults);
                            }
                        }
                        else if (str2 != "HIDE_XSL_OUTPUT")
                        {
                            this.lblMessage.Text = str2;
                        }
                        else
                        {
                            this.lblMessage.Text = DotNetNuke.Services.Localization.Localization.GetString("nooutput", this.LocalResourceFile);
                        }
                    }
                    else
                    {
                        if (this.frmReport.XMLData != null)
                        {
                            SharedFunctions.setStoreCookieValue(this.PortalId, "SQLFORMREPORT", "XMLDATA", this.frmReport.XMLData, 0.0);
                        }
                        this.Response.Write("<script>window.open('" + this.EditUrl("", "", "AdminReportPopup", new string[] { "REPID=" + ReportID.ToString(), "ContainerSrc=" + DotNetNuke.Common.Globals.QueryStringEncode("[G]" + SkinInfo.RootContainer + "/_default/No Container"), "dnnprintmode=true" }) + "','_blank')</script>");
                        this.populateList();
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                this.lblMessage.Text = str + "<br /><hr />";
                ProjectData.ClearProjectError();
            }
        }

        protected virtual NEvoWeb.Modules.NB_Store.AdminReportEdit AdminReportEdit
        {
            get
            {
                return this._AdminReportEdit;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                NEvoWeb.Modules.NB_Store.AdminReportEdit.UpdateButtonEventHandler handler = new NEvoWeb.Modules.NB_Store.AdminReportEdit.UpdateButtonEventHandler(this.AdminReportEdit_UpdateButton);
                NEvoWeb.Modules.NB_Store.AdminReportEdit.DeleteButtonEventHandler handler2 = new NEvoWeb.Modules.NB_Store.AdminReportEdit.DeleteButtonEventHandler(this.AdminReportEdit_DeleteButton);
                NEvoWeb.Modules.NB_Store.AdminReportEdit.CancelButtonEventHandler handler3 = new NEvoWeb.Modules.NB_Store.AdminReportEdit.CancelButtonEventHandler(this.AdminReportEdit_CancelButton);
                NEvoWeb.Modules.NB_Store.AdminReportEdit.AddParamButtonEventHandler handler4 = new NEvoWeb.Modules.NB_Store.AdminReportEdit.AddParamButtonEventHandler(this.AdminReportEdit_AddParamButton);
                if (this._AdminReportEdit != null)
                {
                    this._AdminReportEdit.UpdateButton -= handler;
                    this._AdminReportEdit.DeleteButton -= handler2;
                    this._AdminReportEdit.CancelButton -= handler3;
                    this._AdminReportEdit.AddParamButton -= handler4;
                }
                this._AdminReportEdit = value;
                if (this._AdminReportEdit != null)
                {
                    this._AdminReportEdit.UpdateButton += handler;
                    this._AdminReportEdit.DeleteButton += handler2;
                    this._AdminReportEdit.CancelButton += handler3;
                    this._AdminReportEdit.AddParamButton += handler4;
                }
            }
        }

        protected virtual NEvoWeb.Modules.NB_Store.AdminReportList AdminReportList
        {
            get
            {
                return this._AdminReportList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                NEvoWeb.Modules.NB_Store.AdminReportList.ItemCommandEventHandler handler = new NEvoWeb.Modules.NB_Store.AdminReportList.ItemCommandEventHandler(this.AdminReportList_ItemCommand);
                NEvoWeb.Modules.NB_Store.AdminReportList.AddButtonEventHandler handler2 = new NEvoWeb.Modules.NB_Store.AdminReportList.AddButtonEventHandler(this.AdminReportList_AddButton);
                NEvoWeb.Modules.NB_Store.AdminReportList.SearchButtonEventHandler handler3 = new NEvoWeb.Modules.NB_Store.AdminReportList.SearchButtonEventHandler(this.AdminReportEdit_SearchButton);
                if (this._AdminReportList != null)
                {
                    this._AdminReportList.ItemCommand -= handler;
                    this._AdminReportList.AddButton -= handler2;
                    this._AdminReportList.SearchButton -= handler3;
                }
                this._AdminReportList = value;
                if (this._AdminReportList != null)
                {
                    this._AdminReportList.ItemCommand += handler;
                    this._AdminReportList.AddButton += handler2;
                    this._AdminReportList.SearchButton += handler3;
                }
            }
        }

        protected virtual LinkButton cmdFormReturn
        {
            get
            {
                return this._cmdFormReturn;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdFormReturn_Click);
                if (this._cmdFormReturn != null)
                {
                    this._cmdFormReturn.Click -= handler;
                }
                this._cmdFormReturn = value;
                if (this._cmdFormReturn != null)
                {
                    this._cmdFormReturn.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdFormRun
        {
            get
            {
                return this._cmdFormRun;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdFormRun_Click);
                if (this._cmdFormRun != null)
                {
                    this._cmdFormRun.Click -= handler;
                }
                this._cmdFormRun = value;
                if (this._cmdFormRun != null)
                {
                    this._cmdFormRun.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdResultsReturn
        {
            get
            {
                return this._cmdResultsReturn;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdResultsReturn_Click);
                if (this._cmdResultsReturn != null)
                {
                    this._cmdResultsReturn.Click -= handler;
                }
                this._cmdResultsReturn = value;
                if (this._cmdResultsReturn != null)
                {
                    this._cmdResultsReturn.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgResults
        {
            get
            {
                return this._dgResults;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._dgResults = value;
            }
        }

        protected virtual CustomForm frmReport
        {
            get
            {
                return this._frmReport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._frmReport = value;
            }
        }

        protected virtual Label lblMessage
        {
            get
            {
                return this._lblMessage;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblMessage = value;
            }
        }

        protected virtual Panel pnlEdit
        {
            get
            {
                return this._pnlEdit;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlEdit = value;
            }
        }

        protected virtual Panel pnlForm
        {
            get
            {
                return this._pnlForm;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlForm = value;
            }
        }

        protected virtual Panel pnlList
        {
            get
            {
                return this._pnlList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlList = value;
            }
        }

        protected virtual Panel pnlResults
        {
            get
            {
                return this._pnlResults;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlResults = value;
            }
        }
    }
}

